package com.insurance.controller;

import com.insurance.common.ResponseData;
import com.insurance.common.ResultData;
import com.insurance.pojo.Area_city;
import com.insurance.pojo.Brand;
import com.insurance.pojo.Stores;
import com.insurance.service.RepairShopService;
import com.insurance.service.ShopManageService;
import com.insurance.utils.OutputExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: drc
 * Date: 2020/10/27 9:37
 * Description:
 * Version: V1.0
 */

@Controller
@RequestMapping(value = "/repairShop")
@Api(value = "修理店铺管理信息",tags = "修理店铺管理数据接口API")
public class RepairShopController {
    @Autowired
    private ShopManageService shopManageService;
    @Autowired
    private RepairShopService repairShopService;
    @RequestMapping(value = "/addRepairShop.do",method = RequestMethod.POST)
    @ApiOperation(value = "添加修理店铺信息接口",notes = "根据提交信息添加修理店铺",httpMethod = "post", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="添加成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public ResultData addRepairShop(@RequestBody Stores stores){
        ResultData resultData = new ResultData();
        int result =  shopManageService.addShopManage(stores);
        if(result > 0){
            resultData.setCode(200);
            resultData.setMsg("添加成功");
        }
        return resultData;
    }
    @RequestMapping(value = "/delRepairShop.do",method = RequestMethod.POST)
    @ApiOperation(value = "删除修理店铺信息接口",notes = "根据提交信息删除修理店铺",httpMethod = "post", response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="删除成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public ResultData delRepairShop( int stores_id){
        ResultData resultData = new ResultData();
        int result =  shopManageService.delShopManage(stores_id);
        if(result > 0){
            resultData.setCode(200);
            resultData.setMsg("删除成功");
        }
        return resultData;
    }
    @RequestMapping(value = "/updateRepairShop.do",method = RequestMethod.POST)
    @ApiOperation(value = "修改修理店铺信息接口",notes = "根据提交信息修改修理店铺",httpMethod = "post", response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="修改成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public ResultData updateRepairShop(@RequestBody Stores stores){
        ResultData resultData = new ResultData();
        int result =  shopManageService.updateShopManage(stores);
        if(result > 0){
            resultData.setCode(200);
            resultData.setMsg("修改成功");
        }
        return resultData;
    }
    @RequestMapping(value = "/findRepairShop.do",method = RequestMethod.GET)
    @ApiOperation(value = "查询修理店铺信息接口",notes = "根据提交信息查询修理店铺",httpMethod = "get", response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public ResultData findRepairShop(String stores_name){
        ResultData resultData = new ResultData();
        List<Map> mapList=shopManageService.findShopManage(stores_name);
        resultData.setCode(200);
        resultData.setMsg("查询成功");
        resultData.setData(mapList);
        return resultData;
    }
    @RequestMapping(value = "/OutputExcel.do",method = RequestMethod.GET)
    @ApiOperation(value = "导出修理店excel信息接口",notes = "导出修理店excel",httpMethod = "get", response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="导出成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public String OutputExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html,charset=utf-8");

        List<Stores> list = repairShopService.ShopExcelExport();

        String message = OutputExcel.OutExcel(request, response, list);
        if (message.equals("fail")) {
            ServletOutputStream out = response.getOutputStream();
            message = "导出失败，请重试";
            String s = "<!DOCTYPE HTML><html><head><script>alert('" + message + "');</script></head><body></body></html>";
            out.print(s);
        }



        return message;
    }

}
